/**
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
package com.mbfasttrack.utils;

import java.io.*;
import java.util.*;
import java.util.zip.*;
import org.apache.commons.io.FileUtils;

/**
 * @author Marco Berri
 */
public final class ZipUtil {

    /**
     *
     * @param inputStream
     * @param destinationDir
     * @return ArrayList with file names
     * @throws java.io.IOException
     */
    public static ArrayList<String> unzip(InputStream inputStream, String destinationDir) throws IOException {
        ArrayList<String> result = null;


        final int BUFFER = 2048;

        if (inputStream != null && destinationDir != null) {
            result = new ArrayList<String>();
            ZipInputStream zis = null;
            BufferedOutputStream bos = null;
            try {
                zis = new ZipInputStream(new BufferedInputStream(inputStream));
                ZipEntry entry = null;
                while ((entry = zis.getNextEntry()) != null) { //for each zip entry
                    if (entry.isDirectory()) { //directory

                        File dir = new File(destinationDir + File.separator + entry.getName());
                        FileUtils.forceMkdir(dir);
                        //dir.mkdirs();
                    } else { //file
                        int count;
                        byte data[] = new byte[BUFFER];
                        String fileName = destinationDir + File.separator + entry.getName();

                        File f_make = new File(destinationDir + File.separator + entry.getName());
                        FileUtils.forceMkdir(new File(f_make.getParent()));

                        FileOutputStream fos = new FileOutputStream(fileName);
                        bos = new BufferedOutputStream(fos, BUFFER);
                        while ((count = zis.read(data, 0, BUFFER)) != -1) {
                            bos.write(data, 0, count);
                        }
                        bos.flush();
                        result.add(fileName);
                    }
                }//next zip entry
                zis.close();
            } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException ioe) { /* ignore */ }
                }
                if (zis != null) {
                    try {
                        zis.close();
                    } catch (IOException ioe) { /* ignore */ }
                }
            }
        }//else: input value unavailable

        return result;
    }//unzip()

    public static ArrayList<String> unzip(File f, String destinationDir) throws IOException {
        FileInputStream fis = org.apache.commons.io.FileUtils.openInputStream(f);
        return unzip(fis, destinationDir);
    }
}
